package com.meilai.project.core.excel.config;

import cn.hutool.core.bean.BeanUtil;
import com.meilai.project.core.excel.format.ValueFormat;
import lombok.Data;

/**
 * @author xchen
 * @since 2022-02-15
 */
@SuppressWarnings({"unchecked", "rawtypes"})
@Data
public class TableHeaderConfig {

	//中文名称
	private String label;

	//字段名称
	private String field;

	//宽度
	private Integer width;

	private ValueFormat valueFormat;

	public static TableHeaderConfig of(String label, String field) {
		return of(label, field, null, null);
	}

	public static TableHeaderConfig of(String label, String field, Integer width) {
		return of(label, field, null, width);
	}

	public static TableHeaderConfig of(String label, String field, ValueFormat valueFormat) {
		return of(label, field, valueFormat, null);
	}

	public static TableHeaderConfig of(String label, String field, ValueFormat valueFormat, Integer width) {
		TableHeaderConfig tableHeaderConfig = new TableHeaderConfig();
		tableHeaderConfig.setLabel(label);
		tableHeaderConfig.setField(field);
		tableHeaderConfig.setValueFormat(valueFormat);
		tableHeaderConfig.setWidth(null == width ? null : width * 256);
		return tableHeaderConfig;
	}


	public String getValue(Object config) {
		Object fieldValue = BeanUtil.getFieldValue(config, this.field);
		if (null == fieldValue) {
			return null;
		}
		if (null != this.valueFormat) {
			return this.valueFormat.format(fieldValue, config);
		}
		return fieldValue + "";
	}


}
